﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmSanPham : Form
    {
        DataTable tblSanPham;
        private int flag;
        public frmSanPham()
        {
            InitializeComponent();
            loadDataTable();
            disableControler();
            loadCB();


        }
        private void disableControler()
        {
            txtDonGiaBan.Enabled = false;
            txtNhaSanXuat.Enabled = false;
            txtChatLieu.Enabled = false;
            txtMaSP.Enabled = false;
            txtMauSac.Enabled = false;
            txtTenSP.Enabled = false;
            txtDuongDan.Enabled = false;
            txtdvt.Enabled = false;
            //txtTimKiem.Enabled = false;            
            cbDoTuoi.Enabled = false;
            cbLoaiSP.Enabled = false;
            btnLuu.Enabled = false;
            btnThem.Enabled = true;
            btnFile.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

        }

        private void enableControler()
        {
            txtdvt.Enabled = true;
            txtDonGiaBan.Enabled = true;
            txtNhaSanXuat.Enabled = true;
            txtDuongDan.Enabled = false;
            txtChatLieu.Enabled = true;
            txtMaSP.Enabled = true;
            txtMauSac.Enabled = true;
            txtTenSP.Enabled = true;
            //txtTimKiem.Enabled = true;
            cbDoTuoi.Enabled = true;
            cbLoaiSP.Enabled = true;
            btnThem.Enabled = false;
            btnFile.Enabled = true;

        }
        private void textChange()
        {
            txtDonGiaBan.Text = "";
            txtNhaSanXuat.Text = "";
            txtChatLieu.Text = "";
            txtMaSP.Text = "";
            txtMauSac.Text = "";
            txtTenSP.Text = "";
            txtTimKiem.Text = "";
            txtdvt.Text = "";
            cbDoTuoi.Text = "";
            cbLoaiSP.Text = "";
        }
        private void loadCB()
        {
            //Cb Kho
            //DataTable tblMaKho = KetNoi.GetTable("select * from tblKhoSP");
            //cbbMaKho.DataSource = tblMaKho;

            //cbbMaKho.ValueMember = tblMaKho.Columns["MaKho"].ToString();
            //cbbMaKho.DisplayMember = tblMaKho.Columns["TenKho"].ToString();
            //cbbMaKho.Text = "_____________________________";


            //Cb cc
            //DataTable tblNhacCC = KetNoi.GetTable("select * from tblNhaCungCap");
            //cbMaNhaCC.DataSource = tblNhacCC;

            //cbMaNhaCC.ValueMember = tblNhacCC.Columns["MaNhaCC"].ToString();
            //cbMaNhaCC.DisplayMember = tblNhacCC.Columns["TenNhaCC"].ToString();
            //cbMaNhaCC.Text = "_____________________________";

            //Cb loại sp
            DataTable tblLoaiSP = KetNoi.GetTable("select * from tblLoaiSanPham");
            cbLoaiSP.DataSource = tblLoaiSP;
            cbLoaiSP.ValueMember = tblLoaiSP.Columns["MaLoaiSP"].ToString();
            cbLoaiSP.DisplayMember = tblLoaiSP.Columns["TenLoaiSP"].ToString();
            cbLoaiSP.Text = "_____________________________";

            //Cb do tuoi
            DataTable tblDoTuoi = KetNoi.GetTable("select * from tblDoTuoi");
            cbDoTuoi.DataSource = tblDoTuoi;
            cbDoTuoi.ValueMember = tblDoTuoi.Columns["MaDoTuoi"].ToString();
            cbDoTuoi.DisplayMember = tblDoTuoi.Columns["TenDoTuoi"].ToString();
            cbDoTuoi.Text = "_____________________________";

        }
        private void loadDataTable()
        {
            tblSanPham = KetNoi.GetTable("select * from tblSanPham");
            dtgSanPham.DataSource = tblSanPham;
            dtgSanPham.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            tblSanPham = null;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            txtMaSP.Focus();
            flag = 1;
            btnLuu.Enabled = true;
            textChange();
            enableControler();
            //loadCB();
        }

        private void tbnLuu_Click(object sender, EventArgs e)
        {
            int kq = 0;
            if (flag == 1)
            {
                kq = KetNoi.ExcuteSQL("insert into tblSanPham(MaSP,TenSP,ChatLieu,MauSac,HinhAnh,MaLoaiSP,MaDoTuoi,NhaSanXuat,DonGiaBan,DonViTinh) values ('" +
                             txtMaSP.Text +
                    "',N'" + txtTenSP.Text +
                    "',N'" + txtChatLieu.Text +
                    "',N'" + txtMauSac.Text +
                    "',N'" + txtDuongDan.Text +
                    "',N'" + cbLoaiSP.SelectedValue.ToString() +
                    "',N'" + cbDoTuoi.SelectedValue.ToString() +
                    "',N'" + txtNhaSanXuat.Text +
                    "','" + txtDonGiaBan.Text +
                    "',N'" + txtdvt.Text + "')");
                if (kq > 0)
                {

                    if (MessageBox.Show("Thông tin đã được lưu. Bạn có muốn nhập thêm kho ?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                    }

                    loadDataTable();
                    textChange();
                    kq = 0;

                }
                else
                {
                    MessageBox.Show("Lỗi sảy ra.");
                }
            }
            else
            {

                kq = KetNoi.ExcuteSQL("update tblSanPham set TenSP=N'" + txtTenSP.Text +
                    "', ChatLieu = N'" + txtChatLieu.Text +
                    "', MauSac=N'" + txtMauSac.Text +
                    "', HinhAnh=N'" + txtDuongDan.Text +
                    "', MaLoaiSP=N'" + cbLoaiSP.SelectedValue.ToString() +
                    "', MaDoTuoi=N'" + cbDoTuoi.SelectedValue.ToString() +
                    "', NhaSanXuat=N'" + txtNhaSanXuat.Text +
                    "', DonGiaBan='" + txtDonGiaBan.Text +
                    "', DonViTinh=N'" + txtdvt.Text +
                    "'where MaSP= '" + txtMaSP.Text + "'");
                if (kq > 0)
                {
                    MessageBox.Show("Thông tin đã được sửa.");
                    loadDataTable();
                    kq = 0;
                    disableControler();
                    textChange();
                    //textChange();
                }
                else
                {
                    MessageBox.Show("Lỗi sảy ra.");
                }
            }

        }

        private void dtgSanPham_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow gridrow = dtgSanPham.CurrentRow;
                txtMaSP.Text = gridrow.Cells["MaSP"].Value.ToString();
                txtTenSP.Text = gridrow.Cells["TenSP"].Value.ToString();
                txtNhaSanXuat.Text = gridrow.Cells["NhaSanXuat"].Value.ToString();
                cbDoTuoi.SelectedValue = gridrow.Cells["MaDoTuoi"].Value.ToString();
                txtDonGiaBan.Text = gridrow.Cells["DonGiaBan"].Value.ToString();
                txtMauSac.Text = gridrow.Cells["MauSac"].Value.ToString();
                txtChatLieu.Text = gridrow.Cells["ChatLieu"].Value.ToString();
                txtdvt.Text = gridrow.Cells["DonViTinh"].Value.ToString();
                cbLoaiSP.SelectedValue = gridrow.Cells["MaLoaiSP"].Value.ToString();
                txtDuongDan.Text = gridrow.Cells["HinhAnh"].Value.ToString(); 
                pcbHinhAnh.ImageLocation = gridrow.Cells["HinhAnh"].Value.ToString();
                pcbHinhAnh.SizeMode = PictureBoxSizeMode.StretchImage;
                disableControler();
                txtMaSP.Enabled = false;
                btnSua.Enabled = true;
                btnThem.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName;
                OpenFileDialog OpenFile = new OpenFileDialog();
                OpenFile.InitialDirectory = @"D:\";
                OpenFile.Filter = "File anh (*jpg; *png; *bmp) | *jpg;*png;*bmp";
                if (OpenFile.ShowDialog() == DialogResult.OK)
                {
                    fileName = OpenFile.FileName;

                    txtDuongDan.Text = fileName;
                    pcbHinhAnh.ImageLocation = fileName;
                    pcbHinhAnh.SizeMode = PictureBoxSizeMode.Normal;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            enableControler();
            flag = 2;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int kq = 0;
            kq = KetNoi.ExcuteSQL("delete from tblSanPham where MaSP ='" + txtMaSP.Text + "'");
            if (kq > 0)
            {
                MessageBox.Show("Xóa thông tin sản phẩm thành công");
                loadDataTable();
                textChange();
            }
            else
            {
                MessageBox.Show("Lỗi sảy ra.");
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (txtTimKiem.Text != null)
            {
                tblSanPham = KetNoi.GetTable("select * from tblSanPham where MaSP like '%" + txtTimKiem.Text + "%'");
                dtgSanPham.DataSource = tblSanPham;
            }

        }

        private void frmSanPham_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
